import 'package:backend.core/core/domain/entities/api/google_play/api_google_play_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/api/google_play/api_google_play_transaction_revocation_info.dart';
import 'package:backend.core/core/domain/entities/google_play/google_play_purchased_subscription_transaction.dart';
import 'package:backend.purchases/domain/helpers/api/google_play/create_api_google_play_purchased_item_revocation_info_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/google_play/create_api_google_play_purchased_subscription_offer_type_from_iternal_one.dart';

abstract class CreateApiGooglePlayPurchasedSubscriptionTransactionFromInternalOne {
  ApiGooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayPurchasedSubscriptionTransaction transaction,
  });
}

class CreateApiGooglePlayPurchasedSubscriptionTransactionFromInternalOneImpl
    implements CreateApiGooglePlayPurchasedSubscriptionTransactionFromInternalOne {
  const CreateApiGooglePlayPurchasedSubscriptionTransactionFromInternalOneImpl({
    required CreateApiGooglePlayTransactionRevocationInfoFromInternalOne
        createApiGooglePlayTransactionRevocationInfoFromInternalOne,
    required CreateApiGooglePlayPurchasedSubscriptionTransactionOfferTypeFromInternalOne
        createApiGooglePlayPurchasedSubscriptionTransactionOfferTypeFromInternalOne,
  })  : _createApiGooglePlayTransactionRevocationInfoFromInternalOne =
            createApiGooglePlayTransactionRevocationInfoFromInternalOne,
        _createApiGooglePlayPurchasedSubscriptionTransactionOfferTypeFromInternalOne =
            createApiGooglePlayPurchasedSubscriptionTransactionOfferTypeFromInternalOne;

  final CreateApiGooglePlayTransactionRevocationInfoFromInternalOne
      _createApiGooglePlayTransactionRevocationInfoFromInternalOne;
  final CreateApiGooglePlayPurchasedSubscriptionTransactionOfferTypeFromInternalOne
      _createApiGooglePlayPurchasedSubscriptionTransactionOfferTypeFromInternalOne;

  @override
  ApiGooglePlayPurchasedSubscriptionTransaction call({
    required GooglePlayPurchasedSubscriptionTransaction transaction,
  }) {
    final apiOfferType =
        _createApiGooglePlayPurchasedSubscriptionTransactionOfferTypeFromInternalOne(
      offerType: transaction.offerType,
    );

    ApiGooglePlayTransactionRevocationInfo? apiRevocationInfo;

    if (transaction.revocationInfo != null) {
      apiRevocationInfo = _createApiGooglePlayTransactionRevocationInfoFromInternalOne(
        revocationInfo: transaction.revocationInfo!,
      );
    }

    return ApiGooglePlayPurchasedSubscriptionTransaction(
      orderId: transaction.orderId,
      offerType: apiOfferType,
      expirationTime: transaction.expirationTime,
      time: transaction.time,
      revocationInfo: apiRevocationInfo,
    );
  }
}
